﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CGameApp : MonoBehaviour 
{

    //同步数据库时间
    private float f_SyncInterval = 60F;
    private float f_LastInterval = 0;
    public static CGameApp Instance
    {
        private set;
        get;
    }

    private CMgrList mgrList;


    void Awake()
    {
        DontDestroyOnLoad(gameObject);

        Instance = this;
		_InitUI();
        _InitMgr();

        LoginMgr.Instance.OpenUI();


    }

    void Start()
    {
        f_LastInterval = Time.realtimeSinceStartup;
    }

    void Update()
    {
        if (Time.realtimeSinceStartup > f_LastInterval + f_SyncInterval)
        {
            DataManager.Instance.SyncDB();
            f_LastInterval = Time.realtimeSinceStartup;
        }

        if (Input.GetKeyDown(KeyCode.F))
            UIManager.Instance.UIReview();

    }



    public void OnDestroy()
    {
        mgrList.Destroy();
        mgrList     = null;
        Instance    = null;
    }

    private void _InitUI()
    {
        ResHelper.LoadLocalRes(GlobleConfig.ResPath.UIRoot, ui => {
			GameObject root = ui as GameObject;
			root.name = "UIRoot";

          
            //Transform cam = root.transform.Find("Camera");
            //         cam.GetComponent<Camera>().clearFlags = CameraClearFlags.Depth;
        });
    }

    private void _InitMgr()
    {
        mgrList = new CMgrList();
        mgrList.Init();
    }


    /**
    #region test
    class DBPerson : EntityBase
    {
        public string name;
        public int age;

        public DBPerson()
        { 
        
        }

        public DBPerson(int id, string name, int age)
        {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public override string ToString()
        {
            return string.Format("id:{0} name:{1} age:{2}", id, name, age);
        }
    }

    int i = 1;
    void OnGUI()
    {
        if (GUILayout.Button("test1"))
        {
            //List<TPlot> list = ConfigManager.Instance.GetConfigs<TPlot>();
            //Debugger.LogError(list.Count.ToString());

            DBPerson person = new DBPerson();
            person.age = 10 + i;
            person.name = "aaa" + i;
            i++;
            DataManager.Instance.Add<DBPerson>(ref person);
        }
        else if (GUILayout.Button("SyncDB"))
        {
            DataManager.Instance.SyncDB();
        }
        else if (GUILayout.Button("Query"))
        {
            List<DBPerson> list = DataManager.Instance.QueryList<DBPerson>(DBAdapter.Inst.Add("age", 1, CompareType.Greater));
            Debugger.LogError(list.Count.ToString());
        }
        else if (GUILayout.Button("update"))
        {
            DataManager.Instance.UpdateById<DBPerson>(6, DBAdapter.Inst.Add("name", "张三"));
        }

    }
    #endregion
    */

}
